<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="修改昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <!-- 导航栏 -->
    <!-- 输入框 -->
    <div class="p20">
      <van-field
        v-model="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
    <!-- 输入框 -->
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  components: {},
  data () {
    return {
      localName: this.value
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onClickRight () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })

      if (!this.localName.trim.length === 0) return this.$toast('请输入内容')

      // 发请求
      await updateUserName({
        name: this.localName
      })
      // 关闭弹层
      this.$emit('close')
      // 修改父组件name
      this.$emit('input', this.localName)
      // 提示成功
      this.$toast.success('修改成功')
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
.p20 {
  padding: 20px;
}
</style>
